package com.fita.entity.order.reverse.aviation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.persistence.Embeddable;
import javax.persistence.Transient;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;
import com.demo2do.core.entity.exception.BusinessException;

/**
 * 航班客户填写请求信息
 *
 * @description:
 * @author: 11200
 * @date: 2019/8/21
 */
@Embeddable
public class AviationRequest {

    /**
     * 多个航班用户
     */
    @Type(type = "com.fita.entity.order.reverse.aviation.CommaDelimitedAviationUserInfoType")
    private List<AviationUserInfo> aviationUserInfos;

    /**
     * 备注
     */
    private String aviationRequestRemark;

    /**
     * 航空公司
     */
    private String airline;

    /**
     * 添加一个成员信息
     * @param newAviationUserInfo
     * @return
     */
    public AviationRequest addAviationUserInfo(AviationUserInfo newAviationUserInfo){

        if (StringUtils.isBlank(newAviationUserInfo.getPassportPic())){
            throw new BusinessException("护照图片为空");
        }
        if (this.aviationUserInfos == null){
            this.aviationUserInfos = new ArrayList<>();
        }
        if (aviationUserInfos.stream().filter(info -> info.getPassportPic().equals(newAviationUserInfo.getPassportPic())).findAny().isPresent()){
            throw new BusinessException("相同的护照图片代码");
        }
        this.aviationUserInfos.add(newAviationUserInfo);

        return this;
    }

    /**
     * 添加
     * @param passportPic
     * @return
     */
    @SuppressWarnings("unused")
    public AviationRequest addAviationUserInfo(String passportPic){

        AviationUserInfo aviationUserInfo = new AviationUserInfo(passportPic);

        AviationRequest aviationRequest = addAviationUserInfo(aviationUserInfo);

        return aviationRequest;
    }

    /**
     * 删除一个成员信息
     * @param passportPicCode
     * @return
     */
    public AviationRequest removeAviationUserInfo(String passportPicCode) {

        if (this.aviationUserInfos == null){
            return this;
        }
        List<AviationUserInfo> collect = aviationUserInfos.stream().filter(info -> !info.getPassportPic().equals(passportPicCode)).collect(Collectors.toList());

        this.aviationUserInfos = collect;

        return this;
    }

    /**
     * 修改一个
     * @param aviationUserInfo
     * @return
     */
    public AviationRequest editAviationUserInfo(AviationUserInfo aviationUserInfo){

        if (StringUtils.isBlank(aviationUserInfo.getPassportPic())){
            throw new BusinessException("护照图片为空");
        }
        List<AviationRequest.AviationUserInfo> collect = aviationUserInfos.stream().filter(info -> !info.getPassportPic().equals(aviationUserInfo.getPassportPic())).collect(Collectors.toList());

        collect.add(aviationUserInfo);

        this.aviationUserInfos = collect;

        return this;
    }

    public List<AviationUserInfo> getAviationUserInfos() {
        return aviationUserInfos;
    }

    public void setAviationUserInfos(List<AviationUserInfo> aviationUserInfos) {
        this.aviationUserInfos = aviationUserInfos;
    }

    /**
     * 航班用户
     *
     * @description:
     * @author: 11200
     * @date: 2019/8/30
     */
    public static class AviationUserInfo {

        private String firstName;

        private String lastName;

        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private Date birthday;

        private String passportCode;

        /**
         * 护照到期时间
         */
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private Date passportExpirationTime;

        /**
         * 护照图片
         */
        private String passportPic;

        /**
         * 护照签发国
         */
        private String passportIssuingCountry;

        private String gender;

        /**
         * 国籍
         */
        private String nationality;

        public AviationUserInfo() {
        }

        /**
         *
         * @param passportPic
         */
        public AviationUserInfo(String passportPic) {
            this.passportPic = passportPic;
        }

        @Transient
        public boolean isValid() {
            if (passportExpirationTime == null || birthday == null){
                return false;
            }
            return StringUtils.isNoneBlank(firstName, lastName,passportCode, passportPic, passportIssuingCountry, gender, nationality);
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getPassportCode() {
            return passportCode;
        }

        public void setPassportCode(String passportCode) {
            this.passportCode = passportCode;
        }

        public Date getPassportExpirationTime() {
            return passportExpirationTime;
        }

        public void setPassportExpirationTime(Date passportExpirationTime) {
            this.passportExpirationTime = passportExpirationTime;
        }

        public String getPassportPic() {
            return passportPic;
        }

        public void setPassportPic(String passportPic) {
            this.passportPic = passportPic;
        }

        public String getPassportIssuingCountry() {
            return passportIssuingCountry;
        }

        public void setPassportIssuingCountry(String passportIssuingCountry) {
            this.passportIssuingCountry = passportIssuingCountry;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public String getNationality() {
            return nationality;
        }

        public void setNationality(String nationality) {
            this.nationality = nationality;
        }

        public Date getBirthday() {
            return birthday;
        }

        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    }

    public String getAirline() {
        return airline;
    }

    public void setAirline(String airline) {
        this.airline = airline;
    }

    public String getAviationRequestRemark() {
        return aviationRequestRemark;
    }

    public void setAviationRequestRemark(String aviationRequestRemark) {
        this.aviationRequestRemark = aviationRequestRemark;
    }
}
